import { ref } from 'vue';
import type { Ref } from 'vue';
import { useContext } from '@/hooks';

interface TableData {
  pageData?: any;
  pageSize?: number | undefined;
  pageIndex: number | undefined;
  arrSize?: number | undefined;
}

interface BillContext {
  billNo: Ref<string>;
  setBillNo: (n: string) => void;
  newBill: Ref<boolean>;
  setNewBill: (b: boolean) => void;
  billStatus: Ref<number>;
  setBillStatus: (n: number) => void;
  billDatas: Ref<any>;
  setBillDatas: (d: any) => void;
  refreshBill: Ref<boolean>;
  setRefreshBill: (b: boolean) => void;
  tableData: Ref<TableData>;
  setTableData: (d: TableData) => void;
}

const { useProvide: useBillProvide, useInject: useBillInject } = useContext<BillContext>();

export function useBillContext() {
  const billNo = ref('');
  function setBillNo(n: string) {
    billNo.value = n;
  }

  const newBill = ref(false);
  function setNewBill(b: boolean) {
    newBill.value = b;
  }

  const billStatus = ref(0);
  function setBillStatus(n: number) {
    billStatus.value = n;
  }

  const billDatas = ref(null);
  function setBillDatas(d: any) {
    billDatas.value = d;
  }

  const refreshBill = ref(false);
  function setRefreshBill(b: boolean) {
    refreshBill.value = b;
  }

  const tableData = ref({
    pageData: [],
    pageSize: 0,
    pageIndex: 1,
    arrSize: 0
  });
  async function setTableData(d: any) {
    tableData.value = await d;
  }

  const billContext: BillContext = {
    billNo,
    setBillNo,
    newBill,
    setNewBill,
    billStatus,
    setBillStatus,
    billDatas,
    setBillDatas,
    refreshBill,
    setRefreshBill,
    tableData,
    setTableData
  };

  function useProvide() {
    return useBillProvide(billContext);
  }

  return {
    useProvide,
    useInject: useBillInject
  };
}
